package com.fang.controller;


import com.fang.pojo.JsonResult;
import com.fang.pojo.Place;
import com.fang.service.PlaceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

@SuppressWarnings("all")
@RestController
@RequestMapping("/place")
@CrossOrigin
@Slf4j
public class PlaceController {

    @Autowired
    private PlaceService placeService;

    // 注入配置文件中配置的文件上传路径
    @Value("${upload.dir}")
    private String uploadPath;


    /**
     * 修改景点信息
     * */
    @PostMapping("/update")
    public JsonResult update(MultipartFile pic, Place place) {
        JsonResult jsonResult = new JsonResult();
        try {
            // 对接收的文件做 base64编码
            String picPath = Base64Utils.encodeToString(pic.getBytes());
            place.setPicPath(picPath);
            // 处理文件上传
            String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + extension;
            pic.transferTo(new File(uploadPath, newFileName));
            // 修改景点信息
            placeService.update(place);
            jsonResult.setState(true).setMsg("修改景点信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(false).setMsg(e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 查询一个景点
     * */
    @GetMapping("/findOne")
    public Place findOne(Integer id) {
        return placeService.findOne(id);
    }

    /**
     * 删除景点
     * */
    @GetMapping("/delete")
    public JsonResult delete(Integer id) {
        JsonResult jsonResult = new JsonResult();
        try {
            placeService.delete(id);
            jsonResult.setState(true).setMsg("删除景点成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(false).setMsg("删除景点失败");
        }
        return jsonResult;
    }

    /**
     * 添加景点
     * */
    @PostMapping("/save")
    public JsonResult save(MultipartFile pic, Place place) {
        System.out.println(pic.getOriginalFilename());
        System.out.println(place);
        JsonResult jsonResult = new JsonResult();
        try {
            // 文件上传
            String extension = FilenameUtils.getExtension(pic.getOriginalFilename());
            String newFileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + extension;
            // base64编码
            place.setPicPath(Base64Utils.encodeToString(pic.getBytes()));
            // 文件上传
            File file = new File(uploadPath);
            pic.transferTo(new File(file, newFileName));
            //保存place信息
            placeService.save(place);
            jsonResult.setState(true).setMsg("添加景点成功");
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setState(false).setMsg(e.getMessage());
        }
        return jsonResult;
    }


    /**
     * 查询所有景点
     * */
    @GetMapping("/findAll")
    public Map<String, Object> findAll(Integer page, Integer rows, Integer proviceId) {
        page = page == null ? 1 : page;
        rows = rows == null ? 3 : rows;
        Map<String, Object> map = new HashMap<>();
        // 景点集合
        List<Place> places = placeService.findByProviceIdPage(page, rows, proviceId);
        // 分页处理
        Integer counts = placeService.findByProviceIdCounts(proviceId);
        // 总页数
        Integer totalPage = counts % rows == 0 ? counts / rows : counts / rows + 1;
        map.put("places", places);
        map.put("counts", counts);
        map.put("totalPage", totalPage);
        map.put("page", page);
        return map;
    }
}
